import httpclient from '@ohos/httpclient';
export default {
    http_post:async function (url,data){
        let httpClientImpl = new httpclient.HttpClient.Builder().setConnectTimeout(15, httpclient.TimeUnit.SECONDS).setReadTimeout(15, httpclient.TimeUnit.SECONDS).build();
        let requestBody = httpclient.RequestBody.create(JSON.stringify(data));
        let request = new httpclient.Request.Builder()
            .url(url)
            .method('POST')
            .body(requestBody)
            .addHeader("Content-Type", "application/json")
            .build();

        httpClientImpl.newCall(request).enqueue((result) => {
            console.log("success: " + JSON.stringify(result))
        }, (error) => {
            console.log("error: " + JSON.stringify(error))
        })
    },
    http_post_callback:async function (url,data,callback){

        let httpClientImpl = new httpclient.HttpClient.Builder().setConnectTimeout(15, httpclient.TimeUnit.SECONDS).setReadTimeout(15, httpclient.TimeUnit.SECONDS).build();
        let requestBody = httpclient.RequestBody.create(JSON.stringify(data));
        let request = new httpclient.Request.Builder()
            .url(url)
            .method('POST')
            .body(requestBody)
            .addHeader("Content-Type", "application/json")
            .build();

        httpClientImpl.newCall(request).enqueue((result) => {
            callback((JSON.parse(result.data)).data);
            //console.log("success: " + JSON.stringify(result))
        }, (error) => {
            //console.log("error: " + JSON.stringify(error))
        })
    },
    http_get:async function (url,data){

        let httpClientImpl = new httpclient.HttpClient.Builder().setConnectTimeout(15, httpclient.TimeUnit.SECONDS).setReadTimeout(15, httpclient.TimeUnit.SECONDS).build();
        let requestBody = httpclient.RequestBody.create(JSON.stringify(data));
        let request = new httpclient.Request.Builder()
            .url(this.$app.$def.datas.serverurl+url)
            .method('GET')
            .body(requestBody)
            .addHeader("Content-Type", "application/json")
            .build();
        httpClientImpl.newCall(request).enqueue((result) => {
            console.log("success: " + JSON.stringify(result))
        }, (error) => {
            console.log("error: " + JSON.stringify(error))
        })
    }
}